package com.traffic.between.adaptor;

import java.util.Date;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.jfinal.plugin.activerecord.Record;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.RedisKit;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.WuYikaServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;

@ChooseProvider(channel = "wuyik")
public class WuYikaAdaptor {

	private static Logger log = Logger.getLogger(WuYikaAdaptor.class);

	
	public void order() {
		
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo = orderBean.getCstmOrderNo();
		String callbackURL = BetweenProperties.CALLBACK_URL+"Wuyik";//回调路径
		String operator = orderBean.getOperator();
		if ("3".equals(operator)) {
			operator = "2";
		}else if ("2".equals(operator)) {
			operator = "1";
		}else if ("1".equals(operator)) {
			operator ="3";
		}else {
			
		}
		String FlowType = "";//流量类型 
		String region = orderBean.getRegion();
		if ("0".equals(region)) {
			FlowType = "1";
		}else {
			
			String vasProd = RedisKit.get("vasproduct_"+orderBean.getVasProdId());
			Record vasProdRecord = new Gson().fromJson(vasProd,Record.class);
			log.info("产品信息["+vasProdRecord+"],该订单号为："+cstmOrderNo);
		
			
			if (null!=vasProdRecord) {
				 region = vasProdRecord.getStr("region");
					
					if ("0".equals(region)) {//我们的省漫
						FlowType = "2";
					}else if ("0".equals(region)) {
						FlowType = "3";
					}else {//我们的省内
						
					}
			}
		   
		}

		String result = WuYikaServiceImpl.service.order(productId, phoneNo, cstmOrderNo,operator,FlowType,callbackURL);
		
		JSONObject json = JSONObject.fromObject(result);

		String TranStat = json.getString("TranStat");
	
		
		if ("0000".equals(TranStat)) {
			orderBean.setStatus("11");
			orderBean.setMessage(json.getString("TranInfo"));
			orderBean.setErrorMsg(json.getString("TranInfo"));
			orderBean.setSuppOrderNo(json.getString("TranID"));
		} else {

			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("TranInfo"));
			orderBean.setErrorMsg(json.getString("TranInfo"));
		}		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {
		
		OrderBean orderBean = OrderInfoContext.getContext();

		String cstmOrderNo = orderBean.getIds();
        String phoneNo = orderBean.getPhoneNo();
		String result = WuYikaServiceImpl.service.orderStatus(cstmOrderNo,phoneNo);

		JSONObject json = JSONObject.fromObject(result);

		String TranStat = json.getString("TranStat");

	
		if ("1004".equals(TranStat)||"1011".equals(TranStat)||"0000".equals(TranStat)||"9999".equals(TranStat)) {
			
			orderBean.setStatus("1");
			orderBean.setMessage(json.getString("TranInfo"));
			orderBean.setErrorMsg(json.getString("TranInfo"));
		} else if ("1007".equals(TranStat)) {
			
			orderBean.setStatus("7");
			orderBean.setMessage(json.getString("TranInfo"));
			orderBean.setErrorMsg(json.getString("TranInfo"));
		} else {
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("TranInfo"));
			orderBean.setErrorMsg(json.getString("TranInfo"));
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
